package main.java.test;

import java.sql.*;

public class Main {
    public static void main(String[] args) throws SQLException {
        //测试BeanHandler
        /*String sql="select * from teacher where id=?";
        ResultSetHandler<Teacher> rsh = new BeanHandler(Teacher.class);
        SQLExecutor sqlExecutor = new SQLExecutor(DBUtil.getConnection());
        Teacher teacher = sqlExecutor.executeQuery(sql, rsh, 1);
        System.out.println(teacher.getName());*/

        //测试BeanListHandler
        /*String sql="select * from teacher";
        AbstractListHandler<Teacher> rsh = new BeanListHandler(Teacher.class);
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        List<Teacher> list = sqlExecutor.executeQuery(sql, rsh);
        for (Teacher t:list) {
            System.out.println(t.getName());
        }*/

        //测试ArrayHandler
        /*String sql="select * from teacher where id=?";
        ResultSetHandler<Object[]> rsh = new ArrayHandler();
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        Object[] objects = sqlExecutor.executeQuery(sql, rsh,2);
        for (int i=0;i<objects.length;i++){
            System.out.println(objects[i]);
        }*/

        //测试ArrayListHandler
        /*String sql="select * from teacher";
        AbstractListHandler<Object[]> rsh = new ArrayListHandler();
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        List<Object[]> objects = sqlExecutor.executeQuery(sql, rsh);
        for (Object[] o:objects){
            for (int i=0;i<o.length;i++){
                System.out.println(o[i]);
            }
        }*/

        //测试MapHandler
        /*String sql="select id,name,sex,age from teacher where id=?";
        ResultSetHandler<Map<String,Object>> rsh = new MapHandler();
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        Map<String, Object> map = sqlExecutor.executeQuery(sql, rsh, 1);
        System.out.println(map.values());*/

        //测试MapListHandler
        /*String sql="select * from teacher";
        AbstractListHandler<Map<String,Object>> rsh = new MapListHandler();
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        List<Map<String, Object>> maps = sqlExecutor.executeQuery(sql, rsh);
        for(Map map:maps){
            System.out.println(map.values());
        }*/

        //测试ColumnHandler
        /*String sql="select * from teacher";
        ResultSetHandler rsh = new ColumnHandler(2);
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        Object teacher = sqlExecutor.executeQuery(sql, rsh);
        System.out.println(teacher);*/

        /**
         * 测试ColumnListHandler
         */
        /*String sql="select * from teacher";
        AbstractListHandler<Object> rsh = new ColumnListHandler(2);
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        List<Object> objects = sqlExecutor.executeQuery(sql, rsh);
        for (Object o:objects){
            System.out.println(o);
        }*/

        //System.out.println("-------------------------------------------");

        //测试更新
        /*String sql="update teacher set name=?,sex=?,age=? where id=?";
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        int i = sqlExecutor.executeUpdate(sql, "样富森", "女", "56", 1);
        System.out.println(i);*/

        //测试插入
        /*String sql="insert into teacher(name,sex,age) values(?,?,?)";
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        int i = sqlExecutor.executeUpdate(sql, "样富森1", "女1", "36");
        System.out.println(i);*/

        //测试删除
        /*String sql="delete from teacher where id=?";
        SQLExecutor sqlExecutor =new SQLExecutor(DBUtil.getConnection());
        int i = sqlExecutor.executeUpdate(sql, 4);
        System.out.println(i);*/
    }

    public static class ColumnUtil {

        public static Object columnConvert(ResultSet rs,String columnLabel,Class propType) throws SQLException{
            if (!propType.isPrimitive()&&rs.getObject(columnLabel)==null){
                return null;
            }

            Object value = null;

            if (propType.equals(String.class)){
                value = rs.getString(columnLabel);

            } else if(value instanceof String && propType.isEnum()){
                value = Enum.valueOf(propType, rs.getString(columnLabel));

            } else if (propType.equals(Integer.TYPE) || propType.equals(Integer.class)) {
                value = Integer.valueOf(rs.getInt(columnLabel));

            }else if(propType.equals(Boolean.TYPE)||propType.equals(Boolean.class)){
                value = Boolean.valueOf(rs.getBoolean(columnLabel));

            }else if(propType.equals(Long.TYPE)||propType.equals(Long.class)){
                value = Long.valueOf(rs.getLong(columnLabel));

            }else if (propType.equals(Double.TYPE)||propType.equals(Double.class)){
                value = Double.valueOf(rs.getDouble(columnLabel));

            }else if (propType.equals(Float.TYPE)||propType.equals(Float.class)){
                value = Float.valueOf(rs.getFloat(columnLabel));

            }else if (propType.equals(Short.TYPE)||propType.equals(Short.class)){
                value = Short.valueOf(rs.getShort(columnLabel));

            }else if (propType.equals(Byte.TYPE)||propType.equals(Byte.class)){
                value = Byte.valueOf(rs.getByte(columnLabel));

            }else if (propType.equals(SQLXML.class)){
                value = rs.getSQLXML(columnLabel);
            }else{
                value = rs.getObject(columnLabel);

                //处理日期类型
                value = converData(value, propType);
            }
            return value;
        }


        private static Object converData(Object value,Class<?> propType){
            if (value instanceof java.util.Date){
                String type = propType.getName();
                if ("java.sql.Date".equals(type)){
                    value = new java.sql.Date(((java.util.Date)value).getTime());
                }else if ("java.sql.time".equals(type)){
                    value = new java.sql.Time(((java.util.Date)value).getTime());
                }else if ("java.sql.Timestamp".equals(type)){
                    Timestamp ts = (Timestamp)value;
                    int nanos = ts.getNanos();
                    ((Timestamp)value).setNanos(nanos);
                }
            }
            return value;
        }
    }
}
